﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Web;

namespace BDShop.Framework.Utility
{
    public class HttpHelper
    {
        public static string DoGetRequest(string url)
        {
            // Create Request 
            var request = HttpWebRequest.Create(url);

            // set request method 
            request.Method = "GET";

            // set content type 
            request.ContentType = "application/x-www-form-urlencoded";

            // get response for GET request 
            using (var response = request.GetResponse() as HttpWebResponse)
            {
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    var responseStream = response.GetResponseStream();

                    var myStreamReader = new StreamReader(responseStream, Encoding.UTF8);

                    string pageContent = myStreamReader.ReadToEnd();

                    return pageContent;
                }
            }

            return string.Empty;
        }

        public static string DoPostRequest(string url, Dictionary<string, string> postParameters)
        {
            string postData = "";
            var keys = new List<String>(postParameters.Keys);
            keys.Sort();

            for (var i = 0; i < keys.Count; i++)
            {
                var key = keys[i];
                var value = postParameters[key];
                if (i == keys.Count - 1)
                {
                    postData += HttpUtility.UrlEncode(key) + "=" + HttpUtility.UrlEncode(value);
                }
                else
                {
                    postData += HttpUtility.UrlEncode(key) + "=" + HttpUtility.UrlEncode(value) + "&";
                }
            }

            var myHttpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
            myHttpWebRequest.Method = "POST";

            byte[] data = Encoding.ASCII.GetBytes(postData);

            myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
            myHttpWebRequest.ContentLength = data.Length;

            var requestStream = myHttpWebRequest.GetRequestStream();
            requestStream.Write(data, 0, data.Length);
            requestStream.Close();

            var myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();

            var responseStream = myHttpWebResponse.GetResponseStream();

            var myStreamReader = new StreamReader(responseStream, Encoding.Default);

            string pageContent = myStreamReader.ReadToEnd();

            myStreamReader.Close();
            responseStream.Close();

            myHttpWebResponse.Close();

            return pageContent;
        }

        public static string DoJsonPostRequest(string url, string postData)
        {
            var myHttpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
            myHttpWebRequest.Method = "POST";

            byte[] data = Encoding.ASCII.GetBytes(postData);

            myHttpWebRequest.Accept = "application/json";
            myHttpWebRequest.ContentType = "application/json";
            myHttpWebRequest.ContentLength = data.Length;

            var requestStream = myHttpWebRequest.GetRequestStream();
            requestStream.Write(data, 0, data.Length);
            requestStream.Close();

            var myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();

            var responseStream = myHttpWebResponse.GetResponseStream();

            var myStreamReader = new StreamReader(responseStream, Encoding.Default);

            string pageContent = myStreamReader.ReadToEnd();

            myStreamReader.Close();
            responseStream.Close();

            myHttpWebResponse.Close();

            return pageContent;
        }

        public static string GetIPAddress(HttpRequestBase request)
        {
            string ip;
            try
            {
                ip = request.ServerVariables["HTTP_X_FORWARDED_FOR"];
                if (!string.IsNullOrEmpty(ip))
                {
                    if (ip.IndexOf(",") > 0)
                    {
                        string[] ipRange = ip.Split(',');
                        int le = ipRange.Length - 1;
                        ip = ipRange[le];
                    }
                }
                else
                {
                    ip = request.UserHostAddress;
                }
            }
            catch { ip = null; }

            return ip == "::1" ? "127.0.0.1": ip;
        }
    }
}